package org.liaohailong.victor;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest5 {

    @Test
    public void createTree() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        //      1
        //     2 3
        //  4 5  6 7
        //8 9
        TreeStudy.Node node = TreeStudy.create(arr);
        ArrayList<Integer> result = TreeStudy.print(node);
        log(result);
    }

    @Test
    public void traversal() {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        //      1
        //     2 3
        //  4 5  6 7
        //8 9 .......
        TreeStudy.Node node = TreeStudy.create(arr);
//        TreeStudy.traversal(node);
        TreeStudy.traversal2(node);
    }


    private void log(int[] arr) {
        if (arr.length > 100) {
            int[] shorter = new int[100];
            System.arraycopy(arr, 0, shorter, 0, 100);
            arr = shorter;
        }
        log(Arrays.toString(arr));
    }

    private void log(List<Integer> list) {
        int count = Math.min(100, list.size());

        StringBuilder sb = new StringBuilder();
        sb.append("[");
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            sb.append(next);
            count--;
            if (count <= 0) break;
            if (iterator.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append("]");
        log(sb.toString());
    }

    private void log(String msg, Object... args) {
        String format = String.format(msg, args);
        System.out.println(format);
    }

    private long calc(Runnable runnable) {
        long startMs = System.currentTimeMillis();
        runnable.run();
        return System.currentTimeMillis() - startMs;
    }
}